package com.yhsoft.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yhsoft.common.service.TokenService;
import com.yhsoft.common.vo.Result;
import com.yhsoft.sys.controller.Response.BaicContentByFwxyOutVo;
import com.yhsoft.sys.controller.Response.BaicContentByHomeOutVo;
import com.yhsoft.sys.controller.Response.BaicContentByYszcOutVo;
import com.yhsoft.sys.controller.Response.BaicContentOutVo;
import com.yhsoft.sys.controller.request.BaicContentInVo;
import com.yhsoft.sys.entity.BaicContent;
import com.yhsoft.sys.service.IBaicContentService;
import com.yhsoft.wxprogram.controller.response.FirstPageImgOutVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 内容配置表 前端控制器
 * </p>
 *
 * @author yyyyyyyh
 * @since 2023-10-10
 */
@Api(tags = {"E-内容配置(温馨提示)相关接口"})
@RestController
@RequestMapping("/sys/baicContent")
public class BaicContentController {

    @Resource
    private IBaicContentService iBaicContentService;

    @ApiOperation("pc内容配置")
    @PostMapping("/addBaicContent")
    public Result<?> addBaicContent(@RequestBody BaicContentInVo inVo) {
        if (inVo.getId() == null) {
            iBaicContentService.addBaicContent(inVo);
        } else {
            iBaicContentService.updateBaicContent(inVo);
        }
        return Result.success("配置小程序内容成功！");
    }

    @ApiOperation("pc获得所有内容配置信息")
    @GetMapping("/getAllBaicContentByPC")
    public Result<BaicContentOutVo> getAllBaicContentByPC() {
        BaicContentOutVo outVo = iBaicContentService.getAllBaicContentByPC();
        return Result.success(outVo);
    }


    @ApiOperation("小程序获得所有内容配置信息")
    @GetMapping("/getAllBaicContentByWX")
    public Result<BaicContentOutVo> getAllBaicContentByWX(@RequestParam(value = "appId", required = true) String appId) {
        BaicContentOutVo outVo = iBaicContentService.getAllBaicContentByWX(appId);
        return Result.success(outVo);
    }

    @ApiOperation("小程序获得首页提示内容")
    @GetMapping("/getHomeContentByWX")
    public Result<BaicContentByHomeOutVo> getHomeContentByWX(@RequestParam(value = "appId", required = true) String appId) {
        BaicContentByHomeOutVo outVo = iBaicContentService.getHomeContentByWX(appId);
        return Result.success(outVo);
    }

    @ApiOperation("小程序获得服务协议")
    @GetMapping("/getFwxyByWX")
    public Result<BaicContentByFwxyOutVo> getFwxyByWX(@RequestParam(value = "appId", required = true) String appId) {
        BaicContentByFwxyOutVo outVo = iBaicContentService.getFwxyByWX(appId);
        return Result.success(outVo);
    }

    @ApiOperation("小程序获得隐私政策")
    @GetMapping("/getYszcByWX")
    public Result<BaicContentByYszcOutVo> getYszcByWX(@RequestParam(value = "appId", required = true) String appId) {
        BaicContentByYszcOutVo outVo = iBaicContentService.getYszcByWX(appId);
        return Result.success(outVo);
    }

    @ApiOperation("设置首页图片")
    @GetMapping("/setFirstPageImg")
    public Result<FirstPageImgOutVo> getFirstPageImg(@RequestParam(value = "appPic", required = true) String appPic) {
        LambdaQueryWrapper<BaicContent> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaicContent::getCompanyId, TokenService.getUserBytoken().getCompanyId());
        BaicContent baicContent = iBaicContentService.getBaseMapper().selectOne(queryWrapper);
        if (baicContent == null) {
            return Result.fail("请先完成小程序内容配置!");
        }
        baicContent.setAppPic(appPic);
        iBaicContentService.updateById(baicContent);
        return Result.success("设置首页图片成功!");
    }

    @ApiOperation("pc获取首页图片")
    @GetMapping("/getFirstPageImg")
    public Result<FirstPageImgOutVo> getFirstPageImg() {
        LambdaQueryWrapper<BaicContent> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaicContent::getCompanyId, TokenService.getUserBytoken().getCompanyId());
        BaicContent baicContent = iBaicContentService.getBaseMapper().selectOne(queryWrapper);
        if (baicContent == null) {
            return Result.fail("未配置首页图片!");
        }
        if (StringUtils.isNotEmpty(baicContent.getAppPic())) {
            FirstPageImgOutVo outVo = new FirstPageImgOutVo();
            outVo.setFirstPageImg(baicContent.getAppPic());
            return Result.success(outVo);
        }
        return Result.fail("未配置首页图片!");
    }

    @ApiOperation("小程序获取首页图片")
    @GetMapping("/getFirstPageImgWx")
    public Result<FirstPageImgOutVo> getFirstPageImgWx(@RequestParam(value = "appKey") String appKey) {
        LambdaQueryWrapper<BaicContent> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaicContent::getAppId, appKey);
        BaicContent baicContent = iBaicContentService.getBaseMapper().selectOne(queryWrapper);
        if (baicContent == null) {
            return Result.fail("未配置首页图片!");
        }
        if (StringUtils.isNotEmpty(baicContent.getAppPic())) {
            FirstPageImgOutVo outVo = new FirstPageImgOutVo();
            outVo.setFirstPageImg(baicContent.getAppPic());
            return Result.success(outVo);
        }
        return Result.fail("未配置首页图片!");
    }

}
